// app.js
import { config } from "./config";
import bus from "./plugins/iny-bus.js";
import model from "./plugins/model"
import { loginOpenId, getUserInfo} from "./utils/login"
import { isNull, switchTabBar } from "./utils/index"
import constant from './utils/constant'


wx.$showLoading = function(style){
  model.showLoading()
	//bus.emit('loading',{show: true, style: style, mask: 'black'})
}
wx.$hideLoading = function(){
  model.hideLoading()
	//bus.emit('loading',{show: false})
}
wx.$msg = function(msg){
	model.msg(msg)
}
wx.$success = function(msg){
	model.success(msg)
}
wx.$fail = function(msg){
	model.fail(msg)
}
wx.$confirm = function({content,title,confirmText,confirmColor,cancelText,cancelColor,success}){
	model.confirm({content,title,confirmText,confirmColor,cancelText,cancelColor,success})
}
wx.$isNull = isNull
wx.$basePath = config().baseUrl
wx.$switchTabBar = switchTabBar

App({
  globalData: {
    loading: false,
    loginType: undefined
  },
  onLaunch: function () {
    
    // 小程序更新
    if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function(res) {
        console.log("onCheckForUpdate====", res);
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function() {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function(res) {
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              },
            });
          });
          updateManager.onUpdateFailed(function() {
            // 新的版本下载失败
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
            });
          });
        }
      });
    }

    wx.$showLoading()
		//let menuButtonObject = wx.getMenuButtonBoundingClientRect();
		let that = this;
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
          let capsule = wx.getMenuButtonBoundingClientRect();
          if (capsule) {
            this.globalData.Custom = capsule;
            this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
          } else {
            this.globalData.CustomBar = e.statusBarHeight + 50;
          }
          this.globalData.screenHeight = e.screenHeight
        // 导航高度
        // let statusBarHeight = res.statusBarHeight,
        //   navTop = menuButtonObject.top,
        //   // 胶囊按钮与右侧的距离=windowWidth-right+胶囊宽度
        //   navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width,
        //   navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        //   that.globalData.screenWidth = res.screenWidth
        //   that.globalData.screenHeight = res.screenHeight
				// 	that.globalData.navHeight = navHeight;//导航栏总体高度
				// 	that.globalData.navTop = navTop;//胶囊距离顶部距离
				// 	that.globalData.navObj = menuButtonObject.height;//胶囊高度
				// 	that.globalData.navObjWid = navObjWid;//胶囊宽度（包括右边距离)
				// 	that.globalData.windowWidth = res.windowWidth;
				// 	that.globalData.windowHeight = res.windowHeight;
				// 	that.globalData.Custom = menuButtonObject;
				// 	that.globalData.StatusBar = statusBarHeight + 20;
				// 	if(menuButtonObject){
				// 		that.globalData.CustomBar = menuButtonObject.bottom + menuButtonObject.top - statusBarHeight - 18
				// 	} else {
				// 		that.globalData.CustomBar = statusBarHeight
        //   }
          let isIOS = /ios/gi.test(e.system);
          that.isIOS = isIOS
					
			}
		})

    if(wx.cloud) {
      // 云托管初始化
      wx.cloud.init({
        appid: config().appid,
        envid: config().envid,
        traceUser: true,
      })
		}
		
    loginOpenId(false).then(res=>{
      this.globalData.loginType = res.code
      if(res.code == constant.LOGIN_SUCCESS){
        getUserInfo().then(()=>{
          wx.$hideLoading()
        })
      }else{
        wx.$hideLoading()
      }
    })
	},
  // onLoading: function(){
	// 	this.globalData.loading = true
	// },
	// hideLoading: function(){
	// 	this.globalData.loading = false
	// }

});
